package com.gusha.controller;

import java.math.BigInteger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

import com.gusha.service.ConfigAgentService;
import com.gusha.dao.ConfigAgentDao;
import com.gusha.model.ConfigAgent;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:ConfigAgentController控制类
 */
@Controller
@RequestMapping("/gx/configAgent")
public class ConfigAgentController {

    // 注入Dao
    @Autowired
    private ConfigAgentDao configAgentDao;
    
    // 注入Service
    @Autowired
    private ConfigAgentService configAgentService;

    // 查询列表
    @RequestMapping("list")
    @SysOperateLog(operate = "查看代理商配置列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");

        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        ConfigAgent configAgent = new ConfigAgent();

        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<ConfigAgent> page = configAgentService.findAll(pageable, configAgent);

        model.addAttribute("page", page);
        return "/admin/configAgent/list";
    }

    // 进入添加页面
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入代理商配置添加页面")
    public String toAdd() {

        return "/admin/configAgent/form";
    }

    // 进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入代理商配置编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(configAgentDao.existsById(new BigInteger(id))) {
                ConfigAgent configAgent = configAgentDao.getOne(new BigInteger(id));
                model.addAttribute("obj", configAgent);
            }
        }        

        return "/admin/configAgent/form";
    }

    // 保存
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存代理商配置信息")
    public JsonResult save(ConfigAgent configAgent) {
        JsonResult result = new JsonResult(false,"保存失败");
        configAgentDao.saveAndFlush(configAgent);
        result.setFlag(true);
        result.setMsg("保存成功");

        return result;
    }


    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除代理商配置信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (configAgentDao.existsById(id)) {
            ConfigAgent configAgent = configAgentDao.getOne(id);
            configAgent.setIsDelete(Constants.DELETE);
            configAgentDao.saveAndFlush(configAgent);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }


    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复代理商配置信息")
    public JsonResult recovery(BigInteger id) {
        JsonResult result = new JsonResult(false,"恢复失败");
        if (configAgentDao.existsById(id)) {
            ConfigAgent configAgent = configAgentDao.getOne(id);
            configAgent.setIsDelete(Constants.RECOVERY);
            configAgentDao.saveAndFlush(configAgent);
            result.setFlag(true);
            result.setMsg("恢复成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }

}
